package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.ChargeDto;
import primetest.dto.DoctorDto;
import primetest.dto.TreatmentRecordDto;
import primetest.faces.ChargeListModel;
import primetest.impl.dictionary.Dictionary;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.ChargeService;
import primetest.impl.service.TreatmentRecordService;

@Named("chargeControler")
@SessionScoped
public class ChargeControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -5640687678325697830L;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Inject
	private SessionManager sessionManager;
	private ChargeListModel chargeList = null;
	private List<ChargeDto> baseChargeList;
	private ChargeDto selectedCharge;
	private int chargeListFirst;
	private boolean editMode = false;
	private List<ChargeDto> filteredCharge;
	private float received = 0;
	private float change = 0;

	private DoctorDto doctor;

	@Inject
	private Dictionary dictionary;

	public boolean getEditMode() {
		return editMode;
	}

	@Inject
	private ChargeService chargeService;
	@Inject
	private TreatmentRecordService treatmentRecordService;

	public ChargeListModel getChargeList() {
		if (baseChargeList == null) {
			try {
				baseChargeList = chargeService
							.getChargeList();
				chargeList = new ChargeListModel(
							baseChargeList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseChargeList == null ? new ChargeListModel(
				new ArrayList<ChargeDto>()) : chargeList;
	}

	public ChargeDto getSelectedCharge() {
		return selectedCharge;
	}

	public void setSelectedCharge(
			ChargeDto selectedCharge) {
		this.selectedCharge = selectedCharge;
	}


	public int getChargeListFirst() {
		return chargeListFirst;
	}

	public void setChargeListFirst(int first) {
		this.chargeListFirst = first;
	}

	public void charge() {
		try {
			if (selectedCharge.isValid() && received >= selectedCharge.getFee()) {
				selectedCharge.pay(getDoctor());
				chargeService
						.updateCharge(selectedCharge);
				if (selectedCharge.getType() == ChargeDto.TYPE_TREATMENT) {
					TreatmentRecordDto treatmentRecordDto = treatmentRecordService.getTreatmentRecord(selectedCharge.getSeq());
					treatmentRecordDto.setStatus(TreatmentRecordDto.PAID_STATUS);
					treatmentRecordService.updateTreatmentRecord(treatmentRecordDto);
				}
				editMode = false;
			} else {
				sessionManager.addGlobalMessageFatal("Pay invalid", null);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void calc() {
		change = received - selectedCharge.getFee();
	}

	public void startCharge() {
		editMode = true;
		received = 0;
		change = 0;
	}

	public void cancelCharge() {
		editMode = false;
	}

	public List<ChargeDto> getFilteredCharge() {
		return filteredCharge;
	}

	public void setFilteredCharge(
			List<ChargeDto> filteredCharge) {
		this.filteredCharge = filteredCharge;
	}

	public float getReceived() {
		return received;
	}

	public void setReceived(float received) {
		this.received = received;
	}

	public String getChange() {
		if (change >= 0) {
			return String.valueOf(change);
		} else {
			return dictionary.get("Not enough");
		}
	}

	public void setChange(float change) {
		this.change = change;
	}

	public DoctorDto getDoctor() throws IOException {
		if (doctor == null) {
			doctor = sessionManager.getLoginUser();
		}
		return doctor;
	}

	public void setDoctor(DoctorDto doctor) {
		this.doctor = doctor;
	}

}
